<?php
// $Id$

/**
 * Controller_Search 控制器
 */
class Controller_Search extends Controller_Abstract
{

	function actionIndex()
	{
        // 为 $this->_view 指定的值将会传递数据到视图中
		# $this->_view['text'] = 'Hello!';
		return $this->_redirect("/");
	}
	
	function actionSec(){
		$this->_view['location'] = "sec";
		if(isset($_GET['s']) && $_GET['s'] != '证券搜索'){
			$this->_view['cssfiles'] = array("small");
			
			//如果当前用户没有登录，那么将$user_id设置为-1，这样关注部分就会显示“关注”，而不是“取消关注”
			if(!in_array("member", Q::registry('app')->currentUserRoles())){
				$user_id = -1;
			}else{
				$user = Q::registry("app")->currentUser();
				$user_id = $user['id'];
			}
			
			$s = DB::real_escape(trim($_GET['s']));
			$query = "select sec_id 
				from sec_alias
				where alias = '$s'";
			$res = DB::query($query);
			if(DB::num_rows() >= 1){
				$row = $res->fetch_assoc();
				return $this->_redirect(url("sec/p","id/{$row['sec_id']}"));
			}
			$this->_view['s'] = trim($_GET['s']);

			$query = "
					select name, code, sec.id, sec_sum.follow_cnt, press_cnt, view_cnt
					from sec_alias, sec_sum, sec 
					where sec.id = sec_alias.sec_id and 
						sec_alias.sec_id = sec_sum.id and
						sec_alias.alias like '%$s%' 
                    limit 24
					";
			$res = DB::query($query);
			$rows = array();
			while($row = $res->fetch_assoc()){
				$rows[] = $row;
			}
			$this->_view['find_cnt'] = count($rows);
			$this->_view['secs'] = $rows;
		}else{
			return $this->_redirect("/");
		}
	}
}


